#include "widget.h"
#include "ui_widget.h"
#include <QMessageBox>


Widget::Widget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Widget)
{
    ui->setupUi(this);


    w =new QQuickWidget(this);
    InitVirtualKeyboard(w);
    //ui->lineEdit->setAttribute(Qt::WA_InputMethodEnabled,false);

    ui->lineEdit->installEventFilter(this);
    ui->textEdit->viewport()->installEventFilter(this);

}
void Widget::InitVirtualKeyboard(QQuickWidget * w)
{
    w->setFocusPolicy(Qt::NoFocus);
    w->move (0,600-350);
    w->resize(1024,350);

   // w->move (0,480-280);
   // w->resize(800,280);

    w->setResizeMode(QQuickWidget::SizeRootObjectToView);
    QUrl source(QString("qrc:/%2").arg(MAIN_QML));
    w->setSource(source);
    w->setHidden(true);
}

Widget::~Widget()
{
    delete ui;
}

void Widget::mousePressEvent(QMouseEvent *ev)
{
    HideInputMethod(w);
}


bool  Widget::eventFilter(QObject *obj, QEvent *event)
{
    if((obj==ui->lineEdit)||(obj==ui->textEdit->viewport()))
    {        
        if(event->type()==QEvent::MouseButtonPress)
        {
            if((obj==ui->lineEdit)||(obj==ui->textEdit->viewport()))
            {
                ShowInputMethod(w);
                //ui->lineEdit->setAttribute(Qt::WA_InputMethodEnabled,true);
                //ui->textEdit->setAttribute(Qt::WA_InputMethodEnabled,true);
             }
        }       
    }

    return QWidget::eventFilter(obj,event);
}



void Widget::on_Close_clicked()
{
    this->close();
}
